package com.xiguaji.tiktok.v1.tiktok.mytiktok.repository;

import com.xiguaji.tiktok.v1.tiktok.mytiktok.dto.BloggerRecordResponse;
import com.xiguaji.tiktok.v1.tiktok.mytiktok.dto.MTBloggerResponse;
import com.xiguaji.tiktok.v1.tiktok.mytiktok.model.TGroupBlogger;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface TGroupBloggerRepository extends CrudRepository<TGroupBlogger,Long> {

    List<TGroupBlogger> findByGroupIdAndUserIdOrderByBloggerSortDesc(long groupId,long userId);

    @Modifying
    @Query("DELETE FROM t_group_blogger WHERE group_id = :groupId")
    void deleteByGroupId(long groupId);

    @Modifying
    @Query("DELETE FROM t_group_blogger WHERE user_id = :userId AND uid = :uid")
    void deleteByUserIdAndUid(long userId,long uid);

    @Modifying
    @Query("DELETE FROM t_group_blogger WHERE group_id = :id AND uid = :uid")
    void deleteByGroupIdAndUid(long id,long uid);

    TGroupBlogger findByGroupIdAndUid(long groupId,long uid);

    TGroupBlogger findByUserIdAndUid(long userId,long uid);

    @Query("SELECT b.* FROM t_group_blogger g " +
            "left join t_blogger b on g.user_id = b.user_id and g.uid = b.uid " +
            "where g.user_id = :userId and g.group_id = :groupId order by g.blogger_sort desc ")
    List<MTBloggerResponse> findByLeftJoinTBlogger(long userId, long groupId);

    /**
     * 获取组下my tiktioker播主列表信息(总数据)
     * @param userId
     * @param groupId
     * @return
     */
    @Query("SELECT t2.uid,t3.o_avatar168 avatar,t3.o_region region,t3.custom_verify,t3.enterprise_verify_reason," +
            "t3.o_nickname nickname,t3.o_unique_id unique_id, t3.score,t3.o_follower_count follower_count," +
            "t3.o_aweme_count posts_count, t3.o_total_favorited like_count " +
            "FROM (SELECT t1.uid,t1.blogger_sort FROM t_group_blogger t1 WHERE t1.user_id = :userId and t1.group_id = :groupId) t2 " +
            "LEFT JOIN blogger_main t3 ON t2.uid = t3.o_uid " +
            "ORDER BY t2.blogger_sort DESC limit :limitCount")
    List<BloggerRecordResponse> findBloggersByGroupIdAndUserIdOrderByBloggerSortDesc(long userId, long groupId,int limitCount);
}
